package cn.stronglink.assetmanage.business.inventory.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.CheckDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.dao.InventoryDetailDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventoryUncountModel {
    /**
     *  获取所有未盘点的数据，定1为未盘点状态
     *
     * @return
     */
    public List<Inventory> getDataByOrderNoDistince() {
        List<Inventory> inventoryList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Status.eq(Constants.STATUS_ONE)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    /**
     * 根据rfid查询资产列表
     * @param rfid    扫描到的rfid
     * @param orderNo 表单号
     * @return
     */
    public Map<String, List<InventoryDetail>> getDataByOrderNo(String rfid, String orderNo) {
        InventoryDetail inventoryDetail = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.RfidNo.eq(rfid)).list().get(0);
        if (inventoryDetail == null) {
            InventoryDetail inventoryDetail1More = new InventoryDetail();
            inventoryDetail1More.setRfidNo(rfid);
            inventoryDetail1More.setIsAdd(true);
            DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().insert(inventoryDetail1More);
        } else {
            inventoryDetail.setIsAdd(false);
            DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().insertOrReplace(inventoryDetail);
        }

        //获得二级列表
        Map<String, List<InventoryDetail>> inventoryList = new HashMap<String, List<InventoryDetail>>();
        List<InventoryDetail> inventoryDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(orderNo)).
                where(InventoryDetailDao.Properties.Status.eq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(false)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_normal),
                inventoryDetailList);
        List<InventoryDetail> inventoryDetailList1 = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(orderNo)).
                where(InventoryDetailDao.Properties.Status.eq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(true)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_more),
                inventoryDetailList1);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    /**
     * 根据表单id获取资产列表
     *
     * @param id_
     * @return
     */
    public List<InventoryDetail> getDetailByOrderNo(String id_) {
        List<InventoryDetail> inventoryList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.OrderNo.eq(id_)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    public Inventory getInventoryByOrderNo(String orderNo) {
        Inventory inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Id_.eq(orderNo)).
                        where(InventoryDao.Properties.Status.eq(Constants.STATUS_ONE)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }

    /**
     * todo 点击确定button将，将未盘点的单子改为已经盘点的状态，并给出dialog提示用户.31为未入库，非1为已入库，暂时用2
     * @param inventory
     */
    public void saveCheckByButton(Inventory inventory){
        //将单子列表改为已经入库状态
        List<InventoryDetail> assetList = getDetailByOrderNo(inventory.getId_());
        inventory.setDetailList(assetList);
        inventory.setStatus(Constants.STATUS_TWO);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().insertOrReplace(inventory);
        //将验收单列表保存在任务表中
        Task task = new Task();
        task.setId(inventory.getId());
        task.setId_(inventory.getId_());
        task.setStatus(inventory.getStatus());
        task.setBusinessType(Constants.STATUS_TWO);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTaskDao().insertOrReplace(task);
    }
}
